package com.wxj.bank.storage.config;

import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoClientFactoryBean;
import org.springframework.data.mongodb.core.MongoTemplate;

/**
 * @author shengtao
 * @Description: mongoDb config
 * @date 2019/01/31 17:58
 */
@Configuration
@ConfigurationProperties(prefix = "bank.mongo")
public class MongoDbConfig {

    private final static Logger LOGGER = LoggerFactory.getLogger(MongoDbConfig.class);

    private String host;
    private int port;
    private String db;
    private String username;
    private String password;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getDb() {
        return db;
    }

    public void setDb(String db) {
        this.db = db;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Bean
    public MongoClientFactoryBean mongoClientFactoryBean(){
        MongoClientFactoryBean mongoClientFactoryBean = new MongoClientFactoryBean();
        mongoClientFactoryBean.setHost(host);
        mongoClientFactoryBean.setPort(port);
        MongoCredential[] credentials = new MongoCredential[]{MongoCredential.createCredential(username,db,password.toCharArray())};
        mongoClientFactoryBean.setCredentials(credentials);
        return mongoClientFactoryBean;
    }

    @Bean
    public MongoTemplate mongoTemplate(Mongo mongo){
        MongoTemplate mongoTemplate = new MongoTemplate((MongoClient) mongo,db);
        return mongoTemplate;
    }


}
